package service;

import base.BaseTest;
import com.alibaba.fastjson.JSON;
import com.demo.mapper.CountryMapper;
import com.demo.model.Country;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2017/10/17
 * Time: 11:42
 * function:
 */
public class CommonService extends BaseTest{

    @Autowired
    CountryMapper countryMapper;

    private Country country ;
    private Country countryOne ;

    @Before
    public void init()
    {
        country = new Country();
        country.setCountryName("afafafaf");
        country.setCountryCode("888");
        country.setX(888);

        countryOne = new Country();
        countryOne.setCountryName("dfdfdf");
        countryOne.setCountryCode("999");
        countryOne.setX(999);
    }

    @Test
    public void Tes()
    {

    }

//    @After
    public  void after()
    {
        delete();
    }


    @Test
    public void Test()
    {
//        create();
//        update();
        select();
    }

    private void select()
    {
//        countryMapper.selectByPrimaryKey(country);
//        countryMapper.selectAll();

        Example example = new Example(Country.class);
        example.setOrderByClause("country_name DESC");//注意此处是列名称，不是属性名。
        Example.Criteria criteria = example.createCriteria();
        Country countryOne = new Country();
        countryOne.setCountryName("a");
        countryOne.setCountryCode("1");
        if (StringUtil.isNotEmpty(countryOne.getCountryName())) {
            //注意property和model中对应的property名一致
            criteria.andLike("countryName", "%" + countryOne.getCountryName() + "%");
        }
        if (StringUtil.isNotEmpty(country.getCountryCode())) {
            criteria.andLike("countryCode", "%" + countryOne.getCountryCode() + "%");
        }
        if (country.getId() != null) {
            criteria.andEqualTo("id", countryOne.getId());
        }
        List<Country> list = countryMapper.selectByExample(example);
        System.out.println("---------------->>>" + JSON.toJSONString(list));
    }

    private void delete()
    {
        countryMapper.deleteByPrimaryKey(country);
        countryMapper.deleteByPrimaryKey(countryOne);

        //通过条件进行删除
       // countryMapper.deleteByExample(example);
    }

    private void update() {

        //1.updateByPrimaryKeySelective 通过PrimaryKey条件筛选后，有选择的选Country字段变量不为空的进行修改
        Country tCountry = new Country();
        tCountry.setId(country.getId());
        tCountry.setCountryCode("updateByPrimaryKeySelective");
        countryMapper.updateByPrimaryKeySelective(tCountry);
        System.out.println("---------------->>>" + JSON.toJSONString(countryMapper.selectByPrimaryKey(country)));



        //2.updateByExampleSelective 通过example里的条件筛选后，有选择的选Country字段变量不为空的进行修改
        Example example = new Example(Country.class);
        Example.Criteria criteria = example.createCriteria();
        String name = country.getCountryName();
        criteria.andEqualTo("countryName" , name);

        Country tCountryOne = new Country();
        tCountryOne.setCountryCode("updateByExampleSelective");
        countryMapper.updateByExampleSelective(tCountryOne , example);
        System.out.println("---------------->>>" + JSON.toJSONString(countryMapper.selectByPrimaryKey(country)));

    }

    private void create() {
        countryMapper.insertSelective(country);
        countryMapper.insertSelective(countryOne);

        List<Country> list = countryMapper.select(country);
        List<Country> list1 = countryMapper.select(countryOne);
        System.out.println("---------------->>>" + JSON.toJSONString(list));
        System.out.println("---------------->>>" + JSON.toJSONString(list1));
    }

}
